from PyQt4.QtGui import *

class CDTBufferedGraphics(object):
    def __init__(self):
        self.__init__(QPainter())
        
    def __init__(self, painter):
        self._painter = painter
        
        # pen coord.
        self._penx = 0
        self._peny = 0
        
    def moveTo(self, x, y):
        self._penx = x
        self._peny = y
        
    def lineTo(self, x, y):
        self._painter.drawLine(self._penx, 
                               self._peny,
                               x, 
                               y)
        self._penx = x
        self._peny = y
        
    def setPenColor(self, color):
        self._painter.setPen(color)
        
    def drawRect(self, rc, fore, back):
        self._painter.setPen(fore)
        self._painter.setBrush(back)
        self._painter.drawRect(rc)
        
    def fillRect(self, rc, back):
        self._painter.fillRect(rc, back)
        
    def setClip(self, rc):
        self._painter.setClipRect(rc)
        
    def drawTextNoClip(self, *args):
        r = args.get('rect', QRect(0,0,0,0))
        t = args.get('text', '')
        f = args.get('font', None)
        b = args.get('base', 0)
        r = args.get('fore', QColor.fromRgb(255,255,255))
        k = args.get('back', QColor.fromRgb(0,0,0))
        self.fillRect(r, k)
        self.drawTextTransparent(rect=r,text=t,font=f,base=b,fore=r)
        
    def drawTextClipped(self, *args):
        r = args.get('rect', QRect(0,0,0,0))
        t = args.get('text', '')
        f = args.get('font', None)
        b = args.get('base', 0)
        r = args.get('fore', QColor.fromRgb(255,255,255))
        k = args.get('back', QColor.fromRgb(0,0,0))
        self.setClip(r)
        self.drawTextNoClip(rect=r,text=t,font=f,base=b,fore=r,back=k)
        self._painter.setClipping(False)
        
    def drawTextTransparent(self, *args):
        font = args.get('font', None)
        if Font != None:
            self._painter.setFont(font)
        fore = args.get('fore', QColor.fromRgb(255,255,255))
        self._painter.setPen(fore)
        
        text = args.get('text', '')
        base = args.get('base', 0)
        rect = args.get('rect', QRect(0,0,0,0))
        
        self._painter.drawText(rect.left, base, text)